<?php

/**
 * Controller_quantification
 * 班级量化
 */

class Controller_quantification extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->class    = YOU::loadModel('class');
		$this->model->semester = YOU::loadModel('semester');


	}
    /** 首页 **/
    public function actionIndex()
    {
	
    }

    /** 班级量化列表 **/
    public function actionList ()
    {	
		// 搜索
		$form_time   		= $this->get->form_time;

		$where = " WHERE b.is_show='1' AND a.clas_id=b.clas_id ";
		// 时间查找往日日期的内容
		if(!empty($form_time)){
			$where .= " AND a.form_time = '{$form_time}' ";
			$this->tpl->assign("form_time", $form_time);
		}else{
			// 当今日期的内容
			$form_time = date('Y-m-d');
			$where .= " AND a.form_time = '{$form_time}' ";		
		}

		// 班级
		$class_lists = $this->model->class->getAll();
		// 班级量化名称
        $quan_lists = $this->db->fetchAll("SELECT * FROM `tbl_quantification_clas` WHERE is_show ='1'");
		
		//  生成模板
		foreach($class_lists as $key => $value){
			foreach($quan_lists as $k => $v){
				$data_lists[$value['class_id']]['name'] = $value['class_name'];
				$data_lists[$value['class_id']]['data'][$v['clas_id']] = "";
			}			
		}
		
		$fi_lists =  $this->db->fetchAll("SELECT a.* FROM `tbl_quantification` AS a,`tbl_quantification_clas` AS b {$where}");
		foreach($fi_lists as $key => $value){	
			if(isset($data_lists[$value['class_id']]['data'][$value['clas_id']])){
				$data_lists[$value['class_id']]['data'][$value['clas_id']] = $value['quantification_content'];
			}
		}
		
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("quan_lists", $quan_lists);
        $this->tpl->display('quantification/list.tpl.php');
    }

	/** 班级量化添加  **/
    public function actionAdd()
    {    
		// 班级
		$class_lists = $this->model->class->getAll();
		// 班级量化名称
        $quan_lists = $this->db->fetchAll("SELECT * FROM `tbl_quantification_clas` WHERE is_show ='1'");

		// 获取当前学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		if($_POST){

			$create_time   	= $_POST["create_time"];
			
			foreach($class_lists as $key => $value){
				foreach($quan_lists as $k => $v){
				
					$clas_quantification = $_POST["qu_{$value['class_id']}_{$v['clas_id']}"];
					if(!empty($clas_quantification)){
					// 写入数据库
					$info = array( 

					"class_id"					=> $value['class_id'],
					"clas_id"					=> $v['clas_id'],	
					"quantification_content"    => $clas_quantification,
					"status"					=> 1,
					"semester_id"				=> $semester_id,
					"form_time"					=> $create_time,
					"create_time"				=> date("Y-m-d H:i:s"),
					"update_time"				=> "0000-00-00 00:00:00",  

					 );
					
					$this->db->do_insert("tbl_quantification", $info);
					
				  }
				}		
			
			}
			Helper_Client::location("添加成功", "index.php?a=quantification.add");
		}
		
		$this->tpl->assign("quan_lists", $quan_lists);
		$this->tpl->assign("class_lists", $class_lists);
        $this->tpl->display('quantification/add.tpl.php');
    }

	/** ----------------------------------班级量化名称 --------------------------------------- **/
	/** 班级量化名称列表 **/
    public function actionNameList ()
    {	
		if($_POST){ 
		
			$clas_id   			= $this->post->clas_id;
			// 量化名称
			$quan_lists = $this->db->fetchAll("SELECT * FROM `tbl_quantification_clas`");
			foreach($quan_lists as $key=>$value){
				
				if(in_array($value['clas_id'], $clas_id)){
					$is_show = 1;
			
				}else{
					$is_show = 0;
				}
		
				// 写入数据库
				$info = array( 
					
				"is_show"		=> $is_show,
				"update_time"   => date("Y-m-d H:i:s"),

				 );
					
				$this->db->do_update("tbl_quantification_clas", $info, "clas_id='{$value['clas_id']}'");

			}
			// 生成量化表格跳转页
           Helper_Client::location("生成", "index.php?a=quantification.list");
		}
  
        $quan_lists = $this->db->fetchAll("SELECT * FROM `tbl_quantification_clas` ");
	
		$this->tpl->assign("quan_lists", $quan_lists);
        $this->tpl->display('quantification/name.list.tpl.php');
    }
	/** 班级量化名称添加 **/
    public function actionNameAdd()
    {        
					
        if($_POST){ 

			$clas_quantification   		= $this->post->clas_quantification;

            // 写入数据库
            $info = array( 
				
			"clas_quantification"   => $clas_quantification,
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );
			
          $this->db->do_insert("tbl_quantification_clas", $info);
          Helper_Client::location("添加成功", "index.php?a=quantification.namelist");

         }
         $this->tpl->display("quantification/name.add.tpl.php");
    }
    /** 班级量化修改 **/
    public function actionNameUpdate()
    {   

        $clas_id   = (int)$this->get->clas_id;
		$quan_lists = $this->db->fetchRow("SELECT * FROM `tbl_quantification_clas` WHERE clas_id={$clas_id}");
	
	    if (empty($quan_lists)){
            Helper_Client::alert("信息不存在");
        }

        if ($_POST){ 

			$clas_quantification   	= $this->post->clas_quantification;                    

            // 写入数据库
            $info = array(
				
			"clas_quantification"   => $clas_quantification,				
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );

          $this->db->do_update("tbl_quantification_clas", $info, "clas_id='{$clas_id}'");

          // 跳转
          Helper_Client::location("修改成功", "index.php?a=quantification.namelist");

         }

		$this->tpl->assign("quan_lists", $quan_lists);
	    $this->tpl->display("quantification/name.update.tpl.php");
    } 
	/** 班级量化删除 **/
    public function actionNameDel()
    {
        $clas_id = $this->get->clas_id;   
        $this->db->query("DELETE  FROM `tbl_quantification_clas` WHERE clas_id='{$clas_id}'");
         // 跳转
        Helper_Client::location("删除成功", "index.php?a=quantification.namelist");
    }
	
}

?>